<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\housing;

use think\facade\Db;
use app\common\dao\housing\HousingCateDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class HousingCateRepository
 *
 * @mixin HousingCateDao
 */
class HousingCateRepository extends BaseRepository
{

    public function __construct(HousingCateDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit)
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }


    public function addInfo(array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $userId) {
            $info = $this->create($data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo(0, $userId, 'HOUSING.CATE.ADD', $data);
            return $info;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            $res = $this->update($info['id'], $data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo(0, $userId, 'HOUSING.CATE.EDIT', $data);
            return $res;
        });
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo(0, $userId, 'HOUSING.CATE.DEL', $info);
        return $this->dao->delete($info['id']);
    }


    public function getDetail(int $id)
    {
        return $this->dao->getSearch(['id' => $id])
            ->find();
    }

    public function cateNameExists($cateName)
    {
        $where = [
            'cate_name' => $cateName
        ];
        return $this->dao->existsWhere($where);
    }


    public function getCateData(array $where = [])
    {
        $list = $this->dao->search($where)
            ->field('cate_name,id')
            ->select()->toArray();
        return $list;
    }

    public function getCascaderData(array $where = [])
    {
        $list = $this->dao->search($where)
            ->field('cate_name label,id value')
            ->select()->toArray();
        return $list;
    }

}